package cn.appsys.controller.backend;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.service.backend.UserLoginService;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/backend")
public class BackengUserController {
    @Resource
    private UserLoginService userLoginService;
    /**
     * 分页查询
     * @param map
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "/getlist",method = RequestMethod.GET)
    public  String  getList(ModelMap map, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo
            , HttpSession session,
                            @RequestParam(value="querySoftwareName",required=false) String querySoftwareName,
                            @RequestParam(value="queryStatus",required=false) Integer _queryStatus,
                            @RequestParam(value="queryCategoryLevel1",required=false) Integer _queryCategoryLevel1,
                            @RequestParam(value="queryCategoryLevel2",required=false) Integer _queryCategoryLevel2,
                            @RequestParam(value="queryCategoryLevel3",required=false) Integer _queryCategoryLevel3,
                            @RequestParam(value="queryFlatformId",required=false) Integer _queryFlatformId) throws Exception {
        //查询所属平台querySoftwareName
        List<DataDictionary> flatFormList = userLoginService.appFlatformBiz();
        //调用查询一级分类
        List<AppCategory> appCategories = userLoginService.oneSortBiz();
        //每页显示记录数
        Integer pageSize = 2;
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        //获取所有用户信息
        List<AppInfo> appInfoList = userLoginService.getAppInfoListBiz(querySoftwareName, _queryCategoryLevel1, _queryCategoryLevel2,_queryCategoryLevel3, _queryFlatformId);
        PageInfo<AppInfo> pageInfo = new PageInfo<AppInfo>(appInfoList);
        //分页查询所有
        map.addAttribute("page", pageInfo);
        //一级分类传值到页面
        map.addAttribute("categoryLevel1List", appCategories);
        //所属平台传值到页面
        map.addAttribute("flatFormList", flatFormList);
        /**
         * 分页模糊查询
         */
        //软件名称模糊查询
        map.addAttribute("querySoftwareName",querySoftwareName);
        //所属平台查询
        map.addAttribute("queryFlatformId",_queryFlatformId);
        //一级分页查询
        map.addAttribute("queryCategoryLevel1",_queryCategoryLevel1);
        //二级分页查询
        map.addAttribute("queryCategoryLevel2",_queryCategoryLevel2);
        //三级查询
        map.addAttribute("queryCategoryLevel3",_queryCategoryLevel3);
        return "backend/applist";
    }


    /**
     * 实现ajax根据pid查询
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/categorylevel",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public String categorylevelListAll(Integer pid) {
        AppCategory appCategory = new AppCategory();
        appCategory.setParentId(pid);
        System.out.println("ewqewqe"+appCategory.getParentId() + "\n\n\n");
        //获取二三级分类
        List<AppCategory> appCategories = userLoginService.categoryLevel1ListBzi(appCategory);
        //将ArrayList转换成JSON格式
        return JSONArray.toJSONString(appCategories);
    }

    /**
     * 根据id查询
     * @param id
     * @param vid
     * @param model
     * @return
     */
    @RequestMapping(value="/appche")
    public  String appche(@RequestParam(value = "aid",required = false)  Integer id,@RequestParam("vid") Integer vid , Model model){
        System.err.println("======================");
        AppInfo   appInfo=userLoginService.selectBiz(id ,vid);
        model.addAttribute("appInfo",appInfo);
//        //页面传值所属平台参数flatFormList
//        List<DataDictionary>  appFlatform=devUserService.appFlatformBiz();
//        model.addAttribute("flatFormList",appFlatform);
//        //页面传值一级分类参数categoryLevel1List
//        List<AppCategory> appCategories=devUserService.oneSortBiz();
//        model.addAttribute("categoryLevel1List",appCategories);
//        //页面传值二级分类参数categoryLevel1List
//        List<AppCategory>categoryLevel2List=devUserService.listaddBiz();
//        model.addAttribute("categoryLevel2List",categoryLevel2List);
////         //页面传值二级分类参数categoryLevel1List
////         List<AppCategory>secondary=devUserService.listaddBiz();
////         model.addAttribute("categoryLevel2",categoryLevel2List);
//        //页面传值三级分类参数categoryLevel1List
//        List<AppCategory>secondary=devUserService.Level3Biz();
//        model.addAttribute("categoryLevel3List",secondary);
        return "backend/appcheck";
    }

    /**
     * 根据id修改是否审核通过
     * @param appInfo
     * @return
     */
    @RequestMapping(value = "/updateappche",method = RequestMethod.POST)
    public  String checksave(AppInfo appInfo){
        int count=0;
        //审核通过
        int str=2;
        //审核不通过
        int strid=3;
        //判断status是否为2，为2则表示审核通过
        if (str==appInfo.getStatus()){
            count=userLoginService.updateuserBiz(appInfo);
            return "redirect:/backend/getlist";
        }
        //判断status是否为3，为2则表示审核不通过
        if (strid==appInfo.getStatus()){
            count=userLoginService.updatenoBiz(appInfo);
            return "redirect:/backend/getlist";
        }
        return "backend/appcheck";
    }
}
